package com.css.bjdt_report.report.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_report.annual.entity.RptAnnual;
import com.css.bjdt_report.report.entity.RptGroup;
import com.css.bjdt_report.report.entity.RptGroupAndReportTreeEntity;
import com.css.bjdt_report.report.entity.RptTemplate;
import com.css.bjdt_report.report.entity.ViewRptGroupTemplate;
import com.css.bjdt_report.util.PageQuerys;

@Service
public class ReportManageService {
	@Autowired
	private Dao dao;

	public List<RptGroupAndReportTreeEntity> queryReportByAnnualId(String annualId) {
		List<RptGroupAndReportTreeEntity> RptGroupAndReportTreeEntityList = new ArrayList<RptGroupAndReportTreeEntity>();
		//先来一个根节点
		RptAnnual rptAnnual = dao.fetch(RptAnnual.class, Cnd.where("ANNUALID", "=", annualId));
		RptGroupAndReportTreeEntity rptGroupAndReportTreeEntityGroupA = new RptGroupAndReportTreeEntity();
		rptGroupAndReportTreeEntityGroupA.setGroupOrReportId(annualId);
		rptGroupAndReportTreeEntityGroupA.setGroupOrReportName(rptAnnual.getName());
		rptGroupAndReportTreeEntityGroupA.setIsGroupRpt("2");
		rptGroupAndReportTreeEntityGroupA.setPid("0");
		RptGroupAndReportTreeEntityList.add(rptGroupAndReportTreeEntityGroupA);
		//首先先根据报表版本号获得当前版本的所有报表组,并生成新实体，扔到LIST下
		List<RptGroup> rptGroupList = dao.query(RptGroup.class, Cnd.where("ANNUALID", "=", annualId));
		for (RptGroup rptGroup : rptGroupList) {
			RptGroupAndReportTreeEntity rptGroupAndReportTreeEntityGroup = new RptGroupAndReportTreeEntity();
			rptGroupAndReportTreeEntityGroup.setGroupOrReportId(rptGroup.getGroupId());
			rptGroupAndReportTreeEntityGroup.setGroupOrReportName(rptGroup.getName());
			rptGroupAndReportTreeEntityGroup.setIsGroupRpt("1");
			rptGroupAndReportTreeEntityGroup.setPid(annualId);
			RptGroupAndReportTreeEntityList.add(rptGroupAndReportTreeEntityGroup);
		}
		//再根据套标ID，把每一个套标下面的子表查出来，然后生成新实体，放入LIST下
		for (RptGroup rptGroup : rptGroupList) {
			List<RptTemplate> rptTemplateList = dao.query(RptTemplate.class,
					Cnd.where("GROUPID", "=", rptGroup.getGroupId()).asc("ORDERID"));
			for (RptTemplate rptTemplate : rptTemplateList) {
				RptGroupAndReportTreeEntity rptGroupAndReportTreeEntityGroup = new RptGroupAndReportTreeEntity();
				rptGroupAndReportTreeEntityGroup.setGroupOrReportId(rptTemplate.getTemplateId());
				rptGroupAndReportTreeEntityGroup.setGroupOrReportName(rptTemplate.getName());
				rptGroupAndReportTreeEntityGroup.setPid(rptGroup.getGroupId());
				rptGroupAndReportTreeEntityGroup.setIsGroupRpt("0");
				RptGroupAndReportTreeEntityList.add(rptGroupAndReportTreeEntityGroup);
			}
		}
		return RptGroupAndReportTreeEntityList;
	}

	/**
	 * 分页查询报表组
	 * @param cnd
	 * @return
	 */
	public Map queryPage(Condition cnd, int pageNum, int pageSize) {
		Map result = PageQuerys.ligerGridQuery(dao, RptGroup.class, cnd, pageNum, pageSize);
		/*List<RptGroup> rptGroupList = (List<RptGroup>) result.get("Rows");
		result.put("Rows", rptGroupList);*/
		return result;
	}

	/**
	 * 分页查询该组下的所有子报表
	 * @param cnd
	 * @return
	 */
	public Map queryTemplatePage(Condition cnd, int pageNum, int pageSize) {
		return PageQuerys.ligerGridQuery(dao, ViewRptGroupTemplate.class, cnd, pageNum, pageSize);
	}
}
